package com.SmartCare.controller.client;

import com.SmartCare.common.Result;
import com.SmartCare.common.note.Monitor;
import com.SmartCare.service.ImageService;
import com.SmartCare.service.health.*;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping({"/client","/health"})
@Api(tags = "C端健康检测相关接口")
@Slf4j
public class HealthController {
    @Resource
    private BloodGlucoseService bloodGlucoseService;
    @Resource
    private BloodPressureService bloodPressureService;
    @Resource
    private BodyWeightService bodyWeightService;
    @Resource
    private BodyTemperatureService bodyTemperatureService;
    @Resource
    private BodyHeartRateService bodyHeartRateService;

    @Resource
    private LaboratoryRecordService laboratoryService;

    @Resource
    private VideoReportRecordService videoReportService;

    @Resource
    ImageService imageService;

    @Value("${files.upload.path}/laboratoryImg/")
    private String laboratoryImgUploadPath;

    @Value("${files.upload.path}/videoReportImg/")
    private String videoReportUploadPath;



    /**
     * 血压记录添加
     * @param elderlyId 老人id
     * @param systolicPressure 收缩压
     * @param diastolicPressure 舒张压
     */
    @GetMapping("/bloodPressure/add")
    public Result bloodPressureAdd(@RequestParam Long elderlyId,
                                   @RequestParam(required = false) Float systolicPressure,
                                   @RequestParam(required = false) Float diastolicPressure) {
        return bloodPressureService.bloodPressureAdd(elderlyId,systolicPressure,diastolicPressure);
    }

    /**
     * 体温记录添加
     * @param elderlyId 老人id
     * @param axillaryTemperature 腋温
     * @param oralTemperature 口腔温
     * @param rectalTemperature 肛温
     */
    @GetMapping("/bodyTemperature/add")
    public Result  bodyTemperatureAdd(@RequestParam Long elderlyId,
                                      @RequestParam(required = false) Float axillaryTemperature,
                                      @RequestParam(required = false) Float oralTemperature,
                                      @RequestParam(required = false) Float rectalTemperature) {
        return bodyTemperatureService.bodyTemperatureAdd(elderlyId,axillaryTemperature,oralTemperature,rectalTemperature);
    }

    /**
     * 心率记录添加
     * @param heartRate 心率
     */
    @GetMapping("/heartRate/add")
    public Result heartRateAdd(@RequestParam Long elderlyId,
                               @RequestParam Float heartRate) {
        return bodyHeartRateService.heartRateAdd(elderlyId,heartRate);
    }

    /**
     * 体重记录添加
     */
    @GetMapping("/weight/add")
    public Result weightAdd(@RequestParam Long elderlyId,
                            @RequestParam Float bodyWeight) {
        return bodyWeightService.weightAdd(elderlyId,bodyWeight);
    }

    /**
     * 血糖记录添加
     * @param bloodGlucose 血糖
     * @param moment 时间
     */
    @GetMapping("/bloodGlucose/add")
    public Result bloodGlucoseAdd(@RequestParam Long elderlyId,
                                  @RequestParam Float bloodGlucose,
                                  @RequestParam String moment) {
        return bloodGlucoseService.bloodGlucoseAdd(elderlyId,bloodGlucose,moment);
    }


    /**
     * 血压记录查询
     */
    @GetMapping("/bloodPressure/query")
    @Monitor(description = "一周血压记录查询", module = "健康模块")
    public Result bloodPressureQuery(Long elderlyId,String startTime,String endTime) {
        return bloodPressureService.bloodPressureQuery(elderlyId,startTime,endTime);
    }

    /**
     * 血糖记录查询
     */
    @GetMapping("/bloodGlucose/query")
    @Monitor(description = "血糖记录查询", module = "健康模块")
    public Result bloodGlucoseQuery(Long elderlyId,String moment,String startTime,String endTime) {
        return bloodGlucoseService.bloodGlucoseQuery(elderlyId,moment,startTime,endTime);
    }

    /**
     * 体温信息查询
     */
    @GetMapping("/bodyTemperature/info")
    @Monitor(description = "体温信息查询", module = "健康模块")
    public Result bodyTemperatureQuery(Long elderlyId,String startTime,String endTime){
        return bodyTemperatureService.getTemInfo(elderlyId,startTime,endTime);
    }

    /**
     * 心率记录查询
     */
    @GetMapping("/bodyHeartRate/query")
    @Monitor(description = "心率查询", module = "健康模块")
    public Result bodyHeartRateQueryWeek(Long elderlyId,String startTime,String endTime){
        return bodyHeartRateService.getWeekInfo(elderlyId,startTime,endTime);
    }

    /**
     * 体重查询
     */
    @GetMapping("/bodyWeight")
    @Monitor(description = "体重查询", module = "健康模块")
    public Result bodyWeightQueryWeek(Long id,String startTime,String endTime){
        return bodyWeightService.bodyWeightQueryWeek(id,startTime,endTime);
    }

    /**
     *添加化验单图片
     */
    @PostMapping("/laboratory/upload")
    @Operation(summary = "上传图片文件", description = "将返回该图片的下载地址")
    public Result laboratoryAddImage(@RequestParam List<MultipartFile> file) {
        return imageService.upload(file, laboratoryImgUploadPath, "health/laboratory");
    }

    /**
     * 下载化验单图片
     */
    @GetMapping("/laboratory/{fileUuid}")
    @Operation(summary = "上传图片文件", description = "直接访问该路径可下载图片")
    public void laboratoryDownload(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
        imageService.download(fileUuid, laboratoryImgUploadPath, response);
    }

    /**
     * 添加影像报告
     */
    @PostMapping("/videoReport/upload")
    @Operation(summary = "上传图片文件", description = "将返回该图片的下载地址")
    public Result addVideoReportImage(@RequestParam List<MultipartFile> file) {
        return imageService.upload(file, videoReportUploadPath, "health/videoReport");
    }

    /**
     * 下载影像报告
     */
    @GetMapping("/videoReport/{fileUuid}")
    @Operation(summary = "上传图片文件", description = "直接访问该路径可下载图片")
    public void downloadVideoReport(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
        imageService.download(fileUuid, videoReportUploadPath, response);
    }

    /**
     * 添加化验单信息
     */
    @GetMapping("/laboratory/add")
    public Result addLaboratory(@RequestParam Long elderId, String laboratoryUrl,
                                @RequestParam("testTime") @DateTimeFormat(pattern="yyyy-MM-dd") Date testTime) {
        return  laboratoryService.addLoadVideoReport(elderId,laboratoryUrl,testTime);
    }

    /**
     * 添加影像信息
     */
    @GetMapping("/videoReport/add")
    public Result addVideoReportRecord(@RequestParam Long elderId,
                                      @RequestParam String videoReportUrl,
                                     @RequestParam("testTime") @DateTimeFormat(pattern="yyyy-MM-dd") Date testTime) {
        return  videoReportService.addVideoReportRecord(elderId,videoReportUrl,testTime);
    }
}
